package com.glory.brepda.adapter;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.activity.technicalservice.ChangeWorkOrdersActivity;
import com.glory.brepda.activity.technicalservice.FaultRegisterActivity;
import com.glory.brepda.activity.technicalservice.QCConfirmActivity;
import com.glory.brepda.activity.technicalservice.ReplaceMaterialActivity;
import com.glory.brepda.activity.technicalservice.ReplaceProductActivity;
import com.glory.brepda.activity.technicalservice.ReplaceToolActivity;
import com.glory.brepda.activity.technicalservice.WaitForEightHoursActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.RepairFromListEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.List;

public class RepairFromListAdapter extends BaseQuickAdapter<RepairFromListEntity.RepairData, BaseViewHolder> {
    private boolean mStatus = false;

    private boolean qc_show = false;

    public void setQc_show(boolean qc_show) {
        this.qc_show = qc_show;
    }

    public RepairFromListAdapter(@Nullable List<RepairFromListEntity.RepairData> data, boolean status) {
        super(R.layout.item_repair_from_adapter,data);
        this.mStatus = status;
    }

    @Override
    protected void convert(BaseViewHolder helper, RepairFromListEntity.RepairData item) {
        helper.setText(R.id.tvRepairID,item.getREPAIRSHEETDATA().getSHEETID());
        helper.setText(R.id.tvRepairType,getType(item.getREPAIRSHEETDATA().getREPAIRTYPE()));
        helper.setText(R.id.tvRepairTime,item.getREPAIRSHEETDATA().getTROUBLETIME());
        helper.setText(R.id.tvShiftAndWorkNo,item.getREPAIRSHEETDATA().getSHIFT() + "/" + item.getREPAIRSHEETDATA().getOWNER());
        helper.setText(R.id.tvPriorityAndLotID,item.getREPAIRSHEETDATA().getPRIORITY() + "/" + item.getREPAIRSHEETDATA().getLOTID());
        helper.setText(R.id.tvPositionAndJobNo,item.getREPAIRSHEETDATA().getLOCATION() + "/" + item.getREPAIRSHEETDATA().getOWNER());

        if (item.getREPAIRFORM().getSTATUS().equals("STARTFORM")){
            helper.setTextColor(R.id.tvRepairID, Color.GREEN)
                    .setTextColor(R.id.tvRepairType,Color.GREEN)
                    .setTextColor(R.id.tvRepairTime,Color.GREEN)
                    .setTextColor(R.id.tvShiftAndWorkNo,Color.GREEN)
                    .setTextColor(R.id.tvPriorityAndLotID,Color.GREEN)
                    .setTextColor(R.id.tvPositionAndJobNo,Color.GREEN);
        }

        helper.getView(R.id.llRepairLayout).setOnClickListener(view -> {
            //判断是否可点击
            if (mStatus)
                return;
            //处理故障报修事件，盘点当前状态
            if (item.getREPAIRFORM().getSTATUS().equals("FORM") | item.getREPAIRFORM().getSTATUS().equals("COMPLETE")) {
                return;
            }

            Bundle bundle= new Bundle();
            bundle.putSerializable("REPAIRFROM",item);
            bundle.putBoolean("QC_SHOW",qc_show);

            FreeApi_LogUtils.saveErrorMessages(item.toString(),"REPAIRFROM");
            if (item.getREPAIRFORM().getSTATUS().equals("QC")){
                FreeApi_Utils.skipWithExtraIntent("header", bundle, mContext, QCConfirmActivity.class);
                return;
            }
            //判断维修类型
            switch (item.getREPAIRSHEETDATA().getREPAIRTYPE()){
                case "normal":
                case "eqp":
                case "program":
                    FreeApi_Utils.skipWithExtraIntent("header", bundle, mContext, FaultRegisterActivity.class);
                    break;
                case "changeProducct":
                    FreeApi_Utils.skipWithExtraIntent("header", bundle, mContext, ReplaceProductActivity.class);
                    break;
                case "changeMaterial":
                    FreeApi_Utils.skipWithExtraIntent("header", bundle, mContext, ReplaceMaterialActivity.class);
                    break;
                case "changeTool":
                    FreeApi_Utils.skipWithExtraIntent("header", bundle, mContext, ReplaceToolActivity.class);
                    break;
                case "changeWo":
                    FreeApi_Utils.skipWithExtraIntent("header", bundle, mContext, ChangeWorkOrdersActivity.class);
                    break;
                case "waitingFor8Hour":
                    FreeApi_Utils.skipWithExtraIntent("header", bundle, mContext, WaitForEightHoursActivity.class);
                    break;
                default:
                    FreeApi_ToastUtils.showFreeToast("需求处理",mContext,true,0);
                    break;
            }
        });
    }

    private String getType(String repairtype) {
        String type = "";
        switch (repairtype){
            case "normal":
                type = "一般故障";
                break;
            case "eqp":
                type = "设备故障";
                break;
            case "program":
                type = "程序问题";
                break;
            case "changeProducct":
                type = "换产品";
                break;
            case "changeMaterial":
                type = "换材料";
                break;
            case "changeTool":
                type = "换Tooling";
                break;
            case "changeWo":
                type = "换工单";
                break;
            case "waitingFor8Hour":
                type = "待料8小时";
                break;
            default:
                type = "未知错误";
                break;
        }
        return type;
    }
}
